package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.VoteActivity;
import edu.sctu.yueding.bean.VoteBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.SharePreference;


/**
 * Created by zhengsenwen on 2016/9/6.
 */
public class IndexVoteAdapter extends BaseAdapter {

    private ViewHolder viewHolder;

    private Activity activity;
    private List<VoteBean> list;

    public IndexVoteAdapter(Activity activity, List<VoteBean> list) {
        this.activity = activity;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        /**
         * 减少对象的创建
         */
        if (convertView == null) {
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_index_fragment_listview_vote, null);
            viewHolder = new ViewHolder();

            viewHolder.title = (TextView) convertView.findViewById(R.id.item_index_fragment_listView_vote_title);
            viewHolder.deadline = (TextView) convertView.findViewById(R.id.item_index_fragment_listView_vote_deadline);
            viewHolder.voteNumber = (TextView) convertView.findViewById(R.id.item_index_fragment_listView_voted_number);
            viewHolder.vote = (ImageView) convertView.findViewById(R.id.item_index_fragment_listView_vote_image);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        /**
         * 填充内容
         */
        viewHolder.title.setText(list.get(position).getTitle());

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");
        long time = list.get(position).getEndTime();
        String dateString = simpleDateFormat.format(time);

        viewHolder.deadline.setText(dateString);
        viewHolder.voteNumber.setText(list.get(position).getNumberOfVoters() + "");


        /**
         * 页面跳转
         */
        SharePreference.getInstance(activity).setCache(CacheConfig.CACHE_VOTE_ID, list.get(position).getId());

        final Bundle bundle = new Bundle();
        bundle.putString("voteTitle", list.get(position).getTitle());
        bundle.putString("voteContent", list.get(position).getIntroduction());
        bundle.putInt("votePosition", position);

        viewHolder.vote.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                JumpUtils.jumpActivity(activity, VoteActivity.class, bundle);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        TextView title, deadline, voteNumber;
        ImageView vote;
    }
}
